﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Diploma.Models.IRepositories;

namespace Diploma.Models.Repositories
{
    public class ConsultantOfPartRepository : IConsultantOfPartRepository
    {
        private DiplomaEntities dataContext;

        public ConsultantOfPartRepository(DiplomaEntities dataContext)
        {
            this.dataContext = dataContext;
        }

        #region Implementation of IConsultantOfPartRepository

        public void Create(string text)
        {
            var entity = new ConsultantOfPart
                             {
                                 Id = Guid.NewGuid(),
                                 Text = text
                             };
            dataContext.ConsultantOfParts.AddObject(entity);
            dataContext.SaveChanges();
        }

        public ConsultantOfPart Get(Guid id)
        {
            return dataContext.ConsultantOfParts.FirstOrDefault(r => r.Id == id);
        }

        public IEnumerable<ConsultantOfPart> GetAll()
        {
            return dataContext.ConsultantOfParts;
        }

        public void Update(Guid id, string text)
        {
            var entity = Get(id);
            entity.Text = text;
            dataContext.SaveChanges();
        }

        public void Delete(Guid id)
        {
            var entity = Get(id);
            if(entity != null)
            {
                dataContext.ConsultantOfParts.DeleteObject(entity);
                dataContext.SaveChanges();
            }
        }

        #endregion
    }
}
